package com.gzhryc.shared_device.oem.h5.mng.controllers.cdcz;

import com.gzhryc.common.IDTools;
import com.gzhryc.common.StringTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.cdcz.code.dao.db.ChargeSocket;
import com.gzhryc.shared_device.cdcz.code.dao.db.ChargeSocketSlaveDevice;
import com.gzhryc.shared_device.cdcz.code.dao.enums.ChargeSocketSlotEnum;
import com.gzhryc.shared_device.cdcz.code.services.ChargeSocketService;
import com.gzhryc.shared_device.cdcz.code.services.ChargeSocketSlaveDeviceService;
import com.gzhryc.shared_device.cdcz.code.services.ChargeSocketSlotService;
import com.gzhryc.shared_device.cdcz.core.ICDCZNetworkService;
import com.gzhryc.shared_device.cdcz.core.enums.ECDCZCostMode;
import com.gzhryc.shared_device.cdcz.core.models.CDCZChargeCommand;
import com.gzhryc.shared_device.cdcz.core.models.CDCZExecuteResult;
import com.gzhryc.shared_device.oem.code.HttpNetworkServiceUtils;

import javax.servlet.http.HttpServletRequest;

public class ChargeSocketOperateController {

    @Route("/cdcz/device/operate/slot/charge.html")
    public String slotCharge(HttpServletRequest request) {
        request.setAttribute("costModeList", ECDCZCostMode.values());
        return "/cdcz/device/charge.html";
    }

    @Route(value = {"/cdcz/device/operate/slot/charge.do"})
    public AjaxResult doSlotCharge(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "充电插座开启充电失败");
        String deviceSn = WebUtils.getString(request, "deviceSn");
        Integer slotNum = WebUtils.getInteger(request, "slotNum");
        Integer costMode = WebUtils.getInteger(request, "costMode");
        Integer chargeTimes = WebUtils.getInteger(request, "chargeTimes");
        Double chargeElectricity = WebUtils.getDouble(request, "chargeElectricity");
        if (StringTools.isNotBlank(deviceSn) && slotNum != null && costMode != null) {
            ChargeSocket chargeSocket = ChargeSocketService.self().getByDeviceSn(deviceSn);
            if (chargeSocket != null) {
                ICDCZNetworkService networkService = HttpNetworkServiceUtils.getCDCZNetworkService(chargeSocket.getIotNodeId());
                if (networkService != null) {
                    String msgId = IDTools.getTimestampNanoID();
                    CDCZChargeCommand chargeCommand = new CDCZChargeCommand();
                    chargeCommand.setMessageId(msgId);
                    chargeCommand.setDeviceSn(deviceSn);
                    chargeCommand.setCostMode(costMode);
                    chargeCommand.setSlotNum(slotNum);
                    chargeCommand.setChargeTimes(chargeTimes);
                    chargeCommand.setChargeElectricity(chargeElectricity);

                    CDCZExecuteResult executeResult = networkService.charge(chargeCommand);
                    if (executeResult != null) {
                        if (CDCZExecuteResult.SUCCESS.equals(executeResult.getCode())) {
                            result.setCode(DtoConstants.SUCCESS, null);
                            ChargeSocketSlotService.self().toWorking(chargeCommand.getDeviceSn(),chargeCommand.getSlotNum(),null);
                        } else if (StringTools.isNotBlank(executeResult.getMsg())) {
                            result.setMsg(executeResult.getMsg());
                        } else {
                            result.setMsg("发送命令失败");
                        }
                    } else {
                        result.setMsg("充电插座网络服务无响应");
                    }
                } else {
                    result.setMsg("未获取充电插座网络服务");
                }
            }
        }
        return result;
    }

    @Route(value = {"/cdcz/device/operate/search.do"})
    public AjaxResult doSearch(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "充电插座查询失败");
        String deviceSn = WebUtils.getString(request, "deviceSn");
        if (StringTools.isNotBlank(deviceSn)) {
            ChargeSocket chargeSocket = ChargeSocketService.self().getByDeviceSn(deviceSn);
            if (chargeSocket != null) {
                ChargeSocketSlotService.self().checkSlotTotal(chargeSocket);
                ICDCZNetworkService networkService = HttpNetworkServiceUtils.getCDCZNetworkService(chargeSocket.getIotNodeId());
                if (networkService != null) {
                    CDCZExecuteResult executeResult = networkService.searchDevice(deviceSn);
                    if (executeResult != null) {
                        if (CDCZExecuteResult.SUCCESS.equals(executeResult.getCode())) {
                            result.setCode(DtoConstants.SUCCESS, null);
                        } else if (StringTools.isNotBlank(executeResult.getMsg())) {
                            result.setMsg(executeResult.getMsg());
                        } else {
                            result.setMsg("发送命令失败");
                        }
                    } else {
                        result.setMsg("充电插座网络服务无响应");
                    }
                } else {
                    result.setMsg("未获取充电插座网络服务");
                }
            }
        }
        return result;
    }

    @Route(value = {"/cdcz/device/operate/slot/charge/close.do"})
    public AjaxResult doCloseCharge(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "充电插座关闭充电失败");
        String deviceSn = WebUtils.getString(request, "deviceSn");
        Integer slotNum = WebUtils.getInteger(request, "slotNum");
        if (StringTools.isNotBlank(deviceSn) && slotNum != null) {
            ChargeSocket chargeSocket = ChargeSocketService.self().getByDeviceSn(deviceSn);
            if (chargeSocket != null) {
                ICDCZNetworkService networkService = HttpNetworkServiceUtils.getCDCZNetworkService(chargeSocket.getIotNodeId());
                if (networkService != null) {
                    CDCZExecuteResult executeResult = networkService.closeCharge(deviceSn, slotNum);
                    if (executeResult != null) {
                        if (CDCZExecuteResult.SUCCESS.equals(executeResult.getCode())) {
                            result.setCode(DtoConstants.SUCCESS, null);
                        } else if (StringTools.isNotBlank(executeResult.getMsg())) {
                            result.setMsg(executeResult.getMsg());
                        } else {
                            result.setMsg("发送命令失败");
                        }
                    } else {
                        result.setMsg("充电插座网络服务无响应");
                    }
                } else {
                    result.setMsg("未获取充电插座网络服务");
                }
            }
        }
        return result;
    }
    
    @Route(value = {"/cdcz/device/operate/slot/state/update.do"})
    public AjaxResult doUpdateState(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "充电插座修改状态失败");
        String deviceSn = WebUtils.getString(request, "deviceSn");
        Integer slotNum = WebUtils.getInteger(request, "slotNum");
        Integer state = WebUtils.getInteger(request, "state",ChargeSocketSlotEnum.State.InMaintenance.index());
        if (StringTools.isNotBlank(deviceSn) && slotNum != null) {
            if(ChargeSocketSlotService.self().updateState(deviceSn, slotNum, state)) {
            	 result.setCode(DtoConstants.SUCCESS, null);
            }
        }
        return result;
    }

    @Route(value = {"/cdcz/device/operate/restart.do"})
    public AjaxResult doRestart(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "充电插座主机重启失败");
        String deviceSn = WebUtils.getString(request, "deviceSn");
        if (StringTools.isNotBlank(deviceSn)) {
            ChargeSocket chargeSocket = ChargeSocketService.self().getByDeviceSn(deviceSn);
            if (chargeSocket != null) {
                ICDCZNetworkService networkService = HttpNetworkServiceUtils.getCDCZNetworkService(chargeSocket.getIotNodeId());
                if (networkService != null) {
                    CDCZExecuteResult executeResult = networkService.restart(chargeSocket.getDeviceSn());
                    if (executeResult != null) {
                        if (CDCZExecuteResult.SUCCESS.equals(executeResult.getCode())) {
                            result.setCode(DtoConstants.SUCCESS, null);
                        } else if (StringTools.isNotBlank(executeResult.getMsg())) {
                            result.setMsg(executeResult.getMsg());
                        } else {
                            result.setMsg("发送命令失败");
                        }
                    } else {
                        result.setMsg("充电插座网络服务无响应");
                    }
                } else {
                    result.setMsg("未获取充电插座网络服务");
                }
            }
        }
        return result;
    }

    @Route("/cdcz/device/operate/mqtt_connect/change.html")
    public String changeMQTTConnect(HttpServletRequest request) {
        return "/cdcz/device/change_mqtt_connect.html";
    }

    @Route(value = {"/cdcz/device/operate/mqtt_connect/change.do"})
    public AjaxResult doChangeMQTTConnect(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "充电插座主机切换MQTT连接失败");
        String deviceSn = WebUtils.getString(request, "deviceSn");
        String type = WebUtils.getString(request,"type");
        String ip = WebUtils.getString(request,"ip");
        String port = WebUtils.getString(request,"port");
        String userName = WebUtils.getString(request,"userName");
        String userPwd = WebUtils.getString(request,"userPwd");
        if (StringTools.isNotBlank(deviceSn) && StringTools.isNotBlank(type) && StringTools.isNotBlank(ip) && StringTools.isNotBlank(port)
                && StringTools.isNotBlank(userName) && StringTools.isNotBlank(userPwd)) {
            ChargeSocket chargeSocket = ChargeSocketService.self().getByDeviceSn(deviceSn);
            if (chargeSocket != null) {
                ICDCZNetworkService networkService = HttpNetworkServiceUtils.getCDCZNetworkService(chargeSocket.getIotNodeId());
                if (networkService != null) {
                    CDCZExecuteResult executeResult = networkService.changeMQTTConnect(deviceSn,type,ip,port,userName,userPwd);
                    if (executeResult != null) {
                        if (CDCZExecuteResult.SUCCESS.equals(executeResult.getCode())) {
                            result.setCode(DtoConstants.SUCCESS, null);
                        } else if (StringTools.isNotBlank(executeResult.getMsg())) {
                            result.setMsg(executeResult.getMsg());
                        } else {
                            result.setMsg("发送命令失败");
                        }
                    } else {
                        result.setMsg("充电插座网络服务无响应");
                    }
                } else {
                    result.setMsg("未获取充电插座网络服务");
                }
            }
        }
        return result;
    }

    @Route("/cdcz/device/operate/slave_device/bind.html")
    public String addSlaveDevice(HttpServletRequest request) {
        String mainDeviceSn = WebUtils.getString(request,"mainDeviceSn");
        Long total = ChargeSocketSlaveDeviceService.self().countBindByMainDeviceSn(mainDeviceSn);
        if(total != null && total > 0){
            request.setAttribute("bindIndex",total.intValue());
        }else{
            request.setAttribute("bindIndex",1);
        }
        return "/cdcz/device/v2/bind_slave_device.html";
    }

    @Route(value = { "/cdcz/device/operate/slave_device/bind.do" })
    public AjaxResult doAddSlaveDevice(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "绑定充电插座从机失败");
        ChargeSocketSlaveDevice slaveDevice = WebUtils.getParameter(request, ChargeSocketSlaveDevice.class);
        try {
            ChargeSocket chargeSocket = ChargeSocketService.self().getByDeviceSn(slaveDevice.getMainDeviceSn());
            if (chargeSocket != null) {
                ICDCZNetworkService networkService = HttpNetworkServiceUtils.getCDCZNetworkService(chargeSocket.getIotNodeId());
                if (networkService != null) {
                    if (ChargeSocketSlaveDeviceService.self().bind(slaveDevice,networkService)) {
                        result.setCode(DtoConstants.SUCCESS, null);
                    }
                }
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Route("/cdcz/device/operate/slave_device/find.html")
    public String findSlaveDevice(HttpServletRequest request) {
        String mainDeviceSn = WebUtils.getString(request,"mainDeviceSn");
        Long total = ChargeSocketSlaveDeviceService.self().countBindByMainDeviceSn(mainDeviceSn);
        if(total != null && total > 0){
            request.setAttribute("bindIndex",total.intValue());
        }else{
            request.setAttribute("bindIndex",1);
        }
        return "/cdcz/device/v2/find_slave_device.html";
    }

    @Route(value = { "/cdcz/device/operate/slave_device/find.do" })
    public AjaxResult doFindSlaveDevice(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "查询充电插座从机失败");
        String mainDeviceSn = WebUtils.getString(request,"mainDeviceSn");
        Integer bindIndex = WebUtils.getInteger(request,"bindIndex");
        ChargeSocket chargeSocket = ChargeSocketService.self().getByDeviceSn(mainDeviceSn);
        if (chargeSocket != null) {
            ICDCZNetworkService networkService = HttpNetworkServiceUtils.getCDCZNetworkService(chargeSocket.getIotNodeId());
            if (networkService != null) {
                CDCZExecuteResult executeResult = networkService.findSlaveDevice(mainDeviceSn,bindIndex);
                if (executeResult != null) {
                    if (CDCZExecuteResult.SUCCESS.equals(executeResult.getCode())) {
                        result.setCode(DtoConstants.SUCCESS, null);
                    } else if (StringTools.isNotBlank(executeResult.getMsg())) {
                        result.setMsg(executeResult.getMsg());
                    } else {
                        result.setMsg("发送命令失败");
                    }
                } else {
                    result.setMsg("充电插座网络服务无响应");
                }
            }
        }
        return result;
    }

    @Route(value = { "/cdcz/device/operate/slave_device/remove.do" })
    public AjaxResult doRemoveSlaveDevice(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "移除充电插座从机失败");
        String mainDeviceSn = WebUtils.getString(request, "mainDeviceSn");
        try {
            ChargeSocket chargeSocket = ChargeSocketService.self().getByDeviceSn(mainDeviceSn);
            if (chargeSocket != null) {
                ICDCZNetworkService networkService = HttpNetworkServiceUtils.getCDCZNetworkService(chargeSocket.getIotNodeId());
                if (networkService != null) {
                    if (ChargeSocketSlaveDeviceService.self().remove(mainDeviceSn,networkService)) {
                        result.setCode(DtoConstants.SUCCESS, null);
                    }
                }
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }

}
